import { _decorator } from "cc";
import I18nLabel from "./I18nLabel";
import I18nSprite from "./I18nSprite";

export enum LanguageType {
    default,
    zh_cn, //中文简体
    zh_tw, //中文繁体
    en, //英语
    ko, //韩语
    ja, //日语
    de, //德语
    fr, //法语
    es, //西班牙语
    pt, //葡萄牙语
    it, //意大利语 
    be, //白俄罗斯语
    uk, //乌克兰语 
    id, //印度尼西亚语
    ru, //俄语
    ar, //阿拉伯语
    vi, //越南语
}

export default class I18nManager {
    private static _instance: I18nManager = null;
    public static get instance(): I18nManager {
        if (!this._instance) {
            this._instance = new I18nManager();
            this._instance.checkLanguage();
        }
        return this._instance;
    };

    private _language: LanguageType;
    get language() {
        return this._language;
    }
    set language(type: LanguageType) {
        this._language = type;
    }

    /** 多语言 label */
    private _labelArr: I18nLabel[] = [];
    /** 多语言 sprite */
    private _spriteArr: I18nSprite[] = [];
    /** 多语言 language */
    private _languageArr: LanguageType[] = [
        LanguageType.en, LanguageType.ko, LanguageType.ja
    ];

    checkLanguage(){
        let lang = navigator.language;
        lang = lang.split('-')[0];
        switch (lang) {
            case 'ko':
                this.language = LanguageType.ko;
                break;
            case 'ja':
                this.language = LanguageType.ja;
                break;
            default:
                this.language = LanguageType.en;
                break;
        }
    }

    switchLanguage(type: LanguageType) {
        if (type == this.language) return;

        this.language = type;
        this._labelArr.forEach(element => {
            element.switchLanguage(type);
        });
        this._spriteArr.forEach(element => {
            element.switchLanguage(type);
        });
    }

    registerLabel(item: I18nLabel) {
        this._labelArr.push(item);
    }

    registerSprite(item: I18nSprite) {
        this._spriteArr.push(item);
    }
    
    isSupportLanguage(type: LanguageType){
        return this._languageArr.indexOf(type) >= 0;
    }
}
